home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / video / scrollmon / interface.c++ < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  12.8 KB  |  428 lines

  1. /*
  2.  * Copyright (C) 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include "scrollmon.h"
  18.  
  19. Widget glw, toplevel, gfxShell, mainw, frame, menuBar, mainCtrl, speedSb;
  20. Widget btn, chBoxes, rch, rcv, textF, textFrc, stopStartBtn, pulldownMenu;
  21. Widget dialog, restartBtn;
  22. XtAppContext app_context;
  23. struct resources  appResources;
  24. char *defaultCredFile = "credits.txt";
  25. extern Boolean credMotion;
  26. extern Boolean videoBg;
  27. extern Boolean restart;
  28. extern long bgcol;
  29. extern char *gosign[], *stopsign[], *restsign[], *stopmask_bits[];
  30. Pixmap stopPixmap, goPixmap, restartPixmap;
  31. Pixmap stopShapemask, goShapemask, restartShapemask;
  32. XpmAttributes attr;
  33.  
  34.  
  35. #define offset(field) XtOffsetOf(struct resources, field)
  36. static XtResource Resources[] = {
  37.   {"perf", "perf", XtRBoolean, sizeof(Boolean),
  38.      offset(perf), XtRString, "False"}, 
  39.   {"debug", "debug", XtRBoolean, sizeof(Boolean),
  40.      offset(debug), XtRString, "False"}, 
  41.   {"width", "width", XtRInt, sizeof(int),
  42.      offset(winW), XtRImmediate, (XtPointer)640},
  43.   {"height", "height", XtRInt, sizeof(int),
  44.      offset(winH), XtRImmediate, (XtPointer)480},
  45.   {"speed", "speed", XtRInt, sizeof(int),
  46.      offset(speed), XtRImmediate, (XtPointer)1},
  47.   {"help", "help", XtRBoolean, sizeof(Boolean),
  48.      offset(help), XtRString, "False"}, 
  49.   {"fps", "fps", XtRInt, sizeof(int),
  50.      offset(fps), XtRImmediate, (XtPointer)60}
  51. };
  52. #undef offset
  53.  
  54. static XrmOptionDescRec optionDesc[] = {
  55.   { "-perf",     "perf",     XrmoptionNoArg,  (caddr_t) "True" }, 
  56.   { "-debug",    "debug",    XrmoptionNoArg,  (caddr_t) "True" }, 
  57.   { "-speed",    "speed",    XrmoptionSepArg,  NULL },
  58.   { "-width",    "width",    XrmoptionSepArg,  NULL },
  59.   { "-height",   "height",   XrmoptionSepArg,  NULL },
  60.   { "-help",     "help",     XrmoptionNoArg,  (caddr_t) "True" }, 
  61.   { "-fps",      "fps",      XrmoptionSepArg,  NULL }
  62. };
  63.  
  64. static String fallback_resources[] = {
  65. //    "*stopStartBtn*labelPixmap: stop.xbm",
  66.     "*quitBtn*labelString: Quit",
  67.     "*frame*shadowType: SHADOW_IN",
  68.     "*sgiMode: True",
  69.     NULL
  70.     };
  71.  
  72. static GLXconfig glxConfig [] = {
  73.     { GLX_NORMAL, GLX_DOUBLE, TRUE },
  74.     { GLX_NORMAL, GLX_RGB, TRUE },
  75.     { GLX_NORMAL, GLX_ZSIZE, GLX_NOCONFIG },
  76.     { 0, 0, 0 }
  77. };
  78.  
  79.  
  80. void fileCB(Widget, int);
  81.  
  82. extern void readCreditsFile();
  83. extern void grabTextBlocks(Widget, XtPointer , XtPointer);
  84.  
  85. /* Process all Input callbacks*/
  86. static void
  87. quitCB(Widget, XtPointer, XtPointer)
  88. {
  89.     exit(0);
  90. }
  91.  
  92.  
  93. static void
  94. toggleMotionCB(Widget, XtPointer, XtPointer)
  95. {
  96.   Arg args[5];        
  97.   int n ;                
  98.  
  99.   if (credMotion) {
  100.     credMotion = False;
  101.     n = 0;
  102.     XtSetArg(args[n], XmNlabelPixmap, stopPixmap); n++;
  103.     XtSetValues(stopStartBtn, args, n);
  104.   }
  105.   else {
  106.     credMotion = True;
  107.     n = 0;
  108.     XtSetArg(args[n], XmNlabelPixmap, goPixmap); n++;
  109.     XtSetValues(stopStartBtn, args, n);
  110.   }
  111. }
  112.  
  113.  
  114. static void
  115. updateSpeedCB(Widget w, XtPointer, XtPointer)
  116. {
  117.    Arg args[5];        
  118.    int n ;                
  119.  
  120.     n = 0;
  121.     XtSetArg (args[n], XmNvalue, &appResources.speed);  n++;
  122.     XtGetValues (w, args, n);
  123.     if (appResources.speed)
  124.       cout << "projected time to scroll: " << (CredSection::totalCredHeight + 
  125.         appResources.winH) / (appResources.fps * appResources.speed) << " secs"
  126.         << endl;
  127. }
  128.  
  129.  
  130. void
  131. setPixmaps()
  132. {             
  133.    Arg args[5];        
  134.    int n;
  135.    int error = 0;
  136.    int depth =  DefaultDepth(XtDisplay(toplevel), 
  137.                   DefaultScreen(XtDisplay(toplevel)));      
  138.    
  139.    XpmCreatePixmapFromData(XtDisplay(toplevel), XtWindow(toplevel), 
  140.                            stopsign, &stopPixmap, &stopShapemask, &attr);
  141.    if (error)
  142.      cout << "error creating pixmap" << endl;
  143.                
  144.    XpmCreatePixmapFromData(XtDisplay(toplevel), XtWindow(toplevel), 
  145.                            gosign, &goPixmap, &goShapemask, &attr);
  146.    if (error)
  147.      cout << "error creating pixmap" << endl;
  148.  
  149.    XpmCreatePixmapFromData(XtDisplay(toplevel), XtWindow(toplevel), 
  150.                            restsign, &restartPixmap, &restartShapemask, &attr);
  151.    if (error)
  152.      cout << "error creating pixmap" << endl;
  153.  
  154.    n = 0;
  155.    XtSetArg(args[n], XmNlabelType, XmPIXMAP); n++;
  156.    XtSetArg(args[n], XmNlabelPixmap, goPixmap); n++;
  157.    XtSetValues(stopStartBtn, args, n);
  158.  
  159.    n = 0;
  160.    XtSetArg(args[n], XmNlabelType, XmPIXMAP); n++;
  161.    XtSetArg(args[n], XmNlabelPixmap, restartPixmap); n++;
  162.    XtSetValues(restartBtn, args, n);
  163. }
  164.  
  165.  
  166. static void
  167. installColormap(Widget toplevel, Widget glw)
  168. {
  169.     Window windows[2];
  170.     int status;
  171.  
  172.     windows[0] = XtWindow(glw);
  173.     windows[1] = XtWindow(toplevel);
  174.     status = XSetWMColormapWindows(XtDisplay(toplevel), 
  175.                              XtWindow(toplevel), windows, 2);
  176.     if (status)
  177.       printf("error setting window colormaps\n");
  178. }
  179.  
  180.  
  181. static void
  182. usage(char *progname)
  183. {
  184.     static char *help_message[] = {
  185. "where options include:",
  186. "    -p[erf]                          show performance indicator and perf msgs",
  187. "    -d[ebug]                         print debug messages",
  188. "    -f[ps] <frames per second>       set update rate, default = refresh rate",
  189. "    -s[peed] <scan lines>            set speed by # of scan lines to move in y",
  190. "    -w[idth] <# pixels>              window width in pixels (default 640)",
  191. "    -h[eight] <# pixels>             window height in pixels (default 480)",
  192. "    -h[elp]                          print this usage message",
  193. NULL};
  194.     char **s;
  195.  
  196.     printf("usage:\t%s  [-options ...] [credits file]\n", progname);
  197.     for (s= help_message; *s; s++)
  198.       printf("%s\n", *s);
  199.     printf("\n");
  200.     exit(1);
  201. }
  202.  
  203.  
  204. void fileSelectCB(Widget, XtPointer, 
  205.                   XmFileSelectionBoxCallbackStruct *cbs)
  206. {
  207.     char *filename;
  208.     
  209.     if (!XmStringGetLtoR(cbs->value, 
  210.                           XmSTRING_DEFAULT_CHARSET, &filename)) {
  211.        printf("error\n");
  212.     }
  213.     
  214.     strcpy(appResources.credFilename, filename);
  215.     XtFree(filename);
  216.     XtUnmanageChild(dialog);
  217.     XtPopdown(XtParent(dialog));
  218.     sleep(1);
  219.     fileCB(NULL, 1);
  220. }
  221.  
  222.  
  223. void cancelSelectCB(Widget , XtPointer, XtPointer)
  224. {
  225.     XtUnmanageChild(dialog);
  226.     XtPopdown(XtParent(dialog));
  227. }
  228.  
  229.  
  230. void resetScrollCB(Widget , XtPointer, XtPointer)
  231. {
  232.   restart = True;
  233.   cpack(bgcol);
  234.   clear();
  235.   swapbuffers();
  236.  
  237. }
  238.  
  239.  
  240. void fileCB(Widget, int itemNo)
  241. {
  242.     if (itemNo == 0) {
  243.         if (!dialog) {
  244.             dialog = XmCreateFileSelectionDialog(toplevel, "filesb", NULL, 0);
  245.             XtAddCallback(dialog, XmNcancelCallback, cancelSelectCB, NULL);
  246.             XtAddCallback(dialog, XmNokCallback, (XtCallbackProc)fileSelectCB, NULL);
  247.         }
  248.         XtManageChild(dialog);
  249.         XtPopup(XtParent(dialog), XtGrabNone);
  250.     }
  251.       else if (itemNo == 1) {
  252.         restart = True;
  253.         readCreditsFile();
  254.         grabTextBlocks(NULL, NULL, NULL);   
  255.       }
  256.       else if (itemNo == 2)
  257.         quitCB(NULL, NULL, NULL);
  258. }
  259.  
  260.  
  261.  
  262. void
  263. createInterface(int *avalue, char **argv)
  264. {
  265.     Arg args[20];
  266.     int n;
  267.  
  268. #ifdef SYSTYPE_SVR4
  269.   int *argc = avalue;
  270. #else
  271.   Cardinal *argc = (Cardinal *)avalue;
  272. #endif
  273.  
  274.  
  275.     toplevel = XtAppInitialize(&app_context, "Scrollmon", 
  276.                                optionDesc, XtNumber(optionDesc),
  277.                                argc, (String*)argv, 
  278.                                fallback_resources,
  279.                                (ArgList)NULL, 0);
  280.  
  281.     XtGetApplicationResources(toplevel, (XtPointer)&appResources, Resources,
  282.                                XtNumber(Resources), (ArgList) NULL, NULL);
  283.  
  284.     appResources.credFilename = defaultCredFile;
  285.     if (*argc == 2)
  286.       appResources.credFilename = argv[1];
  287.     else if (*argc > 2 || appResources.help)
  288.       usage(argv[0]);     
  289.  
  290.     n = 0;
  291.     XtSetArg(args[n], XmNwidth, appResources.winW); n++;
  292.     XtSetArg(args[n], XmNheight, appResources.winH); n++;
  293.     gfxShell = XtCreatePopupShell("graphics",
  294.                      topLevelShellWidgetClass, toplevel,
  295.                      args, n);
  296.         
  297.     mainw = XtVaCreateManagedWidget("mainForm", xmMainWindowWidgetClass, toplevel, NULL);
  298.     XtManageChild(mainw);
  299.  
  300.     XmString mbFile = XmStringCreateSimple("File");
  301.     XmString mbOptions = XmStringCreateSimple("Options");
  302.     menuBar = XmVaCreateSimpleMenuBar(mainw, "menuBar",
  303.     XmVaCASCADEBUTTON,  mbFile, 'F', 
  304.     XmVaCASCADEBUTTON,  mbOptions, 'O',
  305.     NULL);
  306.     XmStringFree(mbFile);
  307.     XmStringFree(mbOptions);
  308.     
  309.     XmString openfile = XmStringCreateSimple("Open");
  310.     XmString reopenfile = XmStringCreateSimple("Re-open");
  311.     XmString quitfile = XmStringCreateSimple("Quit");
  312.     pulldownMenu = XmVaCreateSimplePulldownMenu(menuBar, "fileMenu", 0, 
  313.                   (XtCallbackProc)fileCB, 
  314.                        XmVaPUSHBUTTON, openfile, 'O', NULL, NULL, 
  315.                        XmVaPUSHBUTTON, reopenfile, 'R', NULL, NULL,
  316.                        XmVaPUSHBUTTON, quitfile, 'Q', NULL, NULL, 
  317.                        NULL);
  318.     XmStringFree(openfile);
  319.     XmStringFree(reopenfile);
  320.     XmStringFree(quitfile);
  321.  
  322.     XmString miVideo = XmStringCreateSimple("Video");
  323.     pulldownMenu = XmVaCreateSimplePulldownMenu(menuBar, "optionsMenu", 1, 
  324.                        (XtCallbackProc)fileCB, 
  325. //                       XmVaPUSHBUTTON, quitfile, 'V', NULL, NULL, 
  326.                        NULL);
  327.     XmStringFree(miVideo);
  328.  
  329.     XtManageChild(menuBar);
  330.  
  331.     n = 0;
  332.     XtSetArg(args[n], XmNentryAlignment, XmALIGNMENT_CENTER); n++;
  333.     XtSetArg(args[n], XmNorientation, XmVERTICAL); n++;
  334.     XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
  335.     XtSetArg(args[n], XmNtopWidget, menuBar); n++;
  336.     XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
  337.     mainCtrl = XmCreateRowColumn(mainw, "mainCtrl", args, n);
  338.     XtManageChild(mainCtrl);
  339.  
  340.     n = 0;
  341.     XtSetArg(args[n], XmNorientation, XmHORIZONTAL); n++;
  342. #ifdef SYSTYPE_SVR4
  343.     XtSetArg(args[n], XmNtitleString, XmStringCreateLocalized("speed")); n++;
  344. #else
  345.     XtSetArg(args[n], XmNtitleString, XmStringCreate("speed", XmSTRING_DEFAULT_CHARSET)); n++;
  346. #endif
  347.     XtSetArg(args[n], XmNvalue, appResources.speed); n++;
  348.     XtSetArg(args[n], XmNshowValue, True); n++;
  349.     XtSetArg(args[n], XmNminimum, 0); n++;
  350.     XtSetArg(args[n], XmNmaximum, 16); n++;
  351.     XtSetArg(args[n], XmNshowArrows, True); n++;
  352.     speedSb = XmCreateScale(mainCtrl, "speedSb", args, n);
  353.     XtManageChild(speedSb);
  354.     XtAddCallback (speedSb, XmNvalueChangedCallback, updateSpeedCB, NULL);
  355.  
  356.     n = 0;
  357.     XtSetArg(args[n], XmNorientation, XmHORIZONTAL); n++;
  358.     rch = XmCreateRowColumn(mainCtrl, "rch", args, n);
  359.     XtManageChild(rch);
  360.     
  361.     n = 0;
  362.     XtSetArg(args[n], XmNorientation, XmVERTICAL); n++;
  363.     chBoxes = XmCreateRowColumn(rch, "chBoxes", args, n);
  364.     XtManageChild(chBoxes);
  365.  
  366.     n = 0;
  367.     btn = XmCreateToggleButton(chBoxes, "Video-in", args, n);
  368.     XtManageChild (btn);
  369.  
  370.     n = 0;
  371.     XtSetArg(args[n], XmNorientation, XmVERTICAL); n++;
  372.     frame = XmCreateFrame(rch, "scrFrame", args, n);
  373.     XtManageChild(frame);
  374.  
  375.     n = 0;
  376.     XtSetArg(args[n], XmNorientation, XmVERTICAL); n++;
  377.     XtSetArg(args[n], XmNentryAlignment, XmALIGNMENT_CENTER); n++;
  378.     rcv = XmCreateRowColumn(frame, "rcv", args, n);
  379.     XtManageChild(rcv);
  380.  
  381.     n = 0;
  382.     btn = XmCreatePushButton(mainCtrl, "quitBtn", args, n);
  383.     XtManageChild (btn);
  384.     XtAddCallback (btn, XmNactivateCallback, quitCB, NULL);
  385.  
  386.  
  387.     stopStartBtn = XmCreatePushButton(rcv, "stopStartBtn", args, n);
  388.     XtVaSetValues(stopStartBtn,
  389.             XmNwidth, 50,
  390.             XmNheight, 50,
  391.             XmNlabelType, XmPIXMAP,
  392.               XmNentryAlignment, XmALIGNMENT_CENTER,
  393.             XmNmarginLeft,  0,
  394.             XmNmarginRight, 0,
  395.             XmNrecomputeSize, False,
  396.             NULL);
  397.     XtManageChild(stopStartBtn);
  398.     XtAddCallback(stopStartBtn, XmNactivateCallback, toggleMotionCB, NULL);
  399.  
  400.     n = 0;
  401.     XtSetArg(args[n], XmNwidth, 50); n++;
  402.     XtSetArg(args[n], XmNheight, 50); n++;
  403.     XtSetArg(args[n], XmNlabelType, XmPIXMAP); n++;
  404.     XtSetArg(args[n], XmNentryAlignment, XmALIGNMENT_CENTER); n++;
  405.     XtSetArg(args[n], XmNrecomputeSize, False); n++;
  406.     restartBtn = XmCreatePushButton(rcv, "restartBtn", args, n);
  407.     XtManageChild(restartBtn);
  408.     XtAddCallback(restartBtn, XmNactivateCallback, resetScrollCB, NULL);
  409.  
  410.     n = 0;
  411.     XtSetArg(args[n], XmNwidth, appResources.winW); n++;
  412.     XtSetArg(args[n], XmNheight, appResources.winH); n++;
  413.     frame = XmCreateFrame (gfxShell, "frame", args, n);
  414.     XtManageChild (frame);
  415.  
  416.     n = 0;
  417.     XtSetArg(args[n], GlxNglxConfig, glxConfig); n++;
  418.     glw = GlxCreateMDraw(frame, "glwidget", args, n);
  419.     XtManageChild (glw);        
  420.  
  421.  
  422.     XtRealizeWidget(toplevel);
  423.     XtPopup(gfxShell, XtGrabNone);
  424.     installColormap(toplevel,glw);
  425.     setPixmaps();
  426. }
  427.  
  428.